<template>
  <el-form label-width="120px">
    <el-form-item label="SKU名称">
      <el-input placeholder="请您输入SKU名称" v-model="skuParams.skuName"></el-input>
    </el-form-item>
    <el-form-item label="SKU价格(元)">
      <el-input placeholder="请您输入SKU价格" type="number" v-model="skuParams.price"></el-input>
    </el-form-item>
    <el-form-item label="SKU重量(g)">
      <el-input placeholder="请您输入SKU重量(g)" type="number" v-model="skuParams.weight"></el-input>
    </el-form-item>
    <el-form-item label="SKU描述">
      <el-input placeholder="请您输入SKU描述" type="textarea" v-model="skuParams.skuDesc"></el-input>
    </el-form-item>
    <el-form-item label="平台属性">
      <el-form :inline="true" label-width="80px">
        <el-form-item style="margin: 5px 0px;" v-for="(attr, index) in attrArr" :key="attr.id" :label="attr.attrName">
          <el-select v-model="attr.attrIdAndValueId" placeholder="请选择" style="width: 180px;">
            <el-option v-for="(item, index) in attr.attrValueList" :key="item.id" :label="item.valueName"
              :value="`${attr.id}:${item.id}`"></el-option>
          </el-select>
        </el-form-item>
      </el-form>
    </el-form-item>
    <el-form-item label="销售属性">
      <el-form :inline="true">
        <el-form-item v-for="(sale, index) in saleArr" :key="sale.id" :label="sale.saleAttrName">
          <el-select v-model="sale.saleIdAndValueId" placeholder="请选择" style="width: 180px;">
            <el-option v-for="(item, index) in sale.spuSaleAttrValueList" :key="item.id" :label="item.saleAttrValueName"
              :value="`${sale.id}:${item.id}`"></el-option>
          </el-select>
        </el-form-item>
      </el-form>
    </el-form-item>
    <el-form-item label="图片名称">
      <el-table border :data="imgArr" ref="table">
        <el-table-column type="selection" align="center" width="80px"></el-table-column>
        <el-table-column label="图片">
          <template #="{ row, $index }">
            <img :src="row.imgUrl" alt="" style="width: 100px;height: 100px;">
          </template>
        </el-table-column>
        <el-table-column label="名称" prop="imgName"></el-table-column>
        <el-table-column label="操作">
          <template #="{ row, $index }">
            <el-button @click="handler(row)" :type="row.isChecked ? 'primary' : 'danger'" size="small">{{ row.isChecked ==
              1 ? '默认图片' : '设置默认' }}</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-form-item>
    <el-form-item>
      <el-button type="primary" @click="save">保存</el-button>
      <el-button @click="cancel">取消</el-button>
    </el-form-item>
  </el-form>
</template>

<script setup lang="ts">
import { nextTick, ref, registerRuntimeCompiler } from 'vue';
import { reqAttrList } from '@/api/product/attr/index';
import { reqSpuImageList, reqSaleAttrList,reqAddSku } from '@/api/product/spu';
import { ElMessage } from 'element-plus';
//引入仓库
import useCategoryStore from '@/stores/category';
const categoryStore = useCategoryStore();

//全部平台属性
const attrArr = ref<any>([]);
//照片墙
const imgArr = ref<any>([]);
//销售属性
const saleArr = ref<any>([]);
//获取table组件
const table = ref();
//添加SKU商品需要收集的参数-10
const skuParams = ref<any>({
  "category3Id": "",//三级分类的ID
  "spuId": "",//spu的ID
  "tmId": "",//spu产品的品牌的ID

  "skuName": "",//sku的名字
  "price": "",//商品的价格
  "weight": "",//商品的重量
  "skuDesc": "",//商品的描述
  "skuAttrValueList": [
  ],
  "skuSaleAttrValueList": [
  ],
  "skuDefaultImg": "",
})
const initSku = async (spu: any) => {
  //收集数据
  skuParams.value.category3Id = spu.category3Id;
  skuParams.value.spuId = spu.id;
  skuParams.value.tmId = spu.tmId;
  const { c1Id, c2Id, c3Id } = categoryStore;
  //获取平台属性方法
  const result1 = await reqAttrList(c1Id as number, c2Id as number, c3Id as number);
  attrArr.value = result1;
  //获取照片墙的数据
  const result2 = await reqSpuImageList(spu.id);
  imgArr.value = result2.map(((item: any, index: any) => {
    return {
      ...item,
      isChecked: index == 0 ? 1 : 0
    }
  }));
  //默认收集第一张图片地址
  skuParams.value.skuDefaultImg = imgArr.value[0].imgUrl;
  //获取table,默认第一行勾选
  nextTick(() => {
    table.value.toggleRowSelection(imgArr.value[0], true);
  })
  //获取销售属性
  const result3 = await reqSaleAttrList(spu.id);
  saleArr.value = result3;
}
//排他思想
const handler = (row: any) => {
  //全部图片归0 
  imgArr.value.forEach((item: any) => {
    item.isChecked = 0;
    table.value.toggleRowSelection(item, false);
  })
  row.isChecked = 1;
  table.value.toggleRowSelection(row, true);
  //收集数据
  skuParams.value.skuDefaultImg = row.imgUrl;
}
//取消按钮的回调
const cancel = () => {
  $emit('updateScene', 0);
}
//保存按钮的回调
const save = async () => {
  //整理参数
  skuParams.value.skuAttrValueList = attrArr.value.reduce((prev: any, next: any) => {
    if (next.attrIdAndValueId) {
      const [attrId, valueId] = next.attrIdAndValueId.split(":");
      prev.push({ attrId, valueId })
    }
    return prev;
  }, []);
  skuParams.value.skuSaleAttrValueList = saleArr.value.reduce((prev: any, next: any) => {
    if (next.saleIdAndValueId) {
      const [saleAttrId, saleAttrValueId] = next.saleIdAndValueId.split(':');
      prev.push({ saleAttrId, saleAttrValueId });
    }
    return prev;
  }, [])
  try {
    //发请求:添加SKU成功
    await reqAddSku(skuParams.value);
    ElMessage({
      type: "success",
      message: '添加成功'
    });
    $emit('updateScene', 0);
  } catch (error) {

  }
}
const $emit = defineEmits(['updateScene']);
//对外暴露方法
defineExpose({
  initSku
})
</script>

<style scoped></style>